package dav.mobile.garuda.components;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

public class Profile {
	//Profile attribute
	private Boolean m_Init;
	private HashMap<Integer, Boolean> m_Places;
	
	//default constructor
	private Profile() {
		Reset();
	}
	
	private void Reset() {
		//Init
		m_Init = false;
		
		//add places
		m_Places = new HashMap<Integer, Boolean>();
		m_Places.put(Constants.GAME_PLACE_PROLOG, false);
		m_Places.put(Constants.GAME_PLACE_EPILOG, false);
		m_Places.put(Constants.GAME_PLACE_BORNEO, false);
		m_Places.put(Constants.GAME_PLACE_BUNAKEN, false);
		m_Places.put(Constants.GAME_PLACE_PRAMBANAN, false);
		m_Places.put(Constants.GAME_PLACE_TOBA, false);
	}
	
	//Singleton holder
	private static class ProfileHolder {
		public static final Profile INSTANCE = new Profile();
	}
	
	//Getter-Setter
	public static Profile getInstance() {
		return ProfileHolder.INSTANCE;
	}
	
	
	public void EnablePlace(int i) {
		m_Places.put(i, true);
	}
	
	public HashMap<Integer, Boolean> getPlaces() {
		return m_Places;
	}
	
	public Boolean hasGameInit() {
		return m_Init;
	}
	
	public void InitGame() {
		m_Init = true;
		
		EnablePlace(Constants.GAME_PLACE_PROLOG);
	}
	
	//Create JSON string
	public String CreateJSON() {
		JSONObject m_Object = new JSONObject();
		
		try {
			//Put story progress
			m_Object.put("story-prolog", m_Places.get(Constants.GAME_PLACE_PROLOG));
			m_Object.put("story-epilog", m_Places.get(Constants.GAME_PLACE_EPILOG));
			m_Object.put("story-prambanan", m_Places.get(Constants.GAME_PLACE_PRAMBANAN));
			m_Object.put("story-bunaken", m_Places.get(Constants.GAME_PLACE_BUNAKEN));
			m_Object.put("story-borneo", m_Places.get(Constants.GAME_PLACE_BORNEO));
			m_Object.put("story-toba", m_Places.get(Constants.GAME_PLACE_TOBA));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return m_Object.toString();
	}
	
	//Load from JSON
	public void LoadJSON(JSONObject m_Object) {
		//Reset
		Reset();
		
		//Get data
		try {
			m_Places.put(Constants.GAME_PLACE_PROLOG, Boolean.parseBoolean(m_Object.get("story-prolog").toString()));
			m_Places.put(Constants.GAME_PLACE_EPILOG, Boolean.parseBoolean(m_Object.get("story-epilog").toString()));
			m_Places.put(Constants.GAME_PLACE_PRAMBANAN, Boolean.parseBoolean(m_Object.get("story-prambanan").toString()));
			m_Places.put(Constants.GAME_PLACE_BUNAKEN, Boolean.parseBoolean(m_Object.get("story-bunaken").toString()));
			m_Places.put(Constants.GAME_PLACE_BORNEO, Boolean.parseBoolean(m_Object.get("story-borneo").toString()));
			m_Places.put(Constants.GAME_PLACE_TOBA, Boolean.parseBoolean(m_Object.get("story-toba").toString()));
			
			m_Init = (m_Places.get(Constants.GAME_PLACE_PROLOG)) ? true : false;
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
